package com.cskaoyan.market.service.wx.impl;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.*;
import com.cskaoyan.market.service.wx.WxHomeService;
import com.cskaoyan.market.vo.wx.lhh.homeIndexVo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WxHomeServiceImpl
 * @Description
 * @Author MZ
 * @Date 2023/5/20 14:15
 */
@Service
public class WxHomeServiceImpl implements WxHomeService {
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketCouponMapper couponMapper;
    @Autowired
    MarketCategoryMapper categoryMapper;
    @Autowired
    MarketAdMapper adMapper;
    @Autowired
    MarketBrandMapper brandMapper;
    @Autowired
    MarketTopicMapper topicMapper;

    @Override
    public Map<String, Object> getIndex() {
        Map<String, Object> result = new HashMap<>();

        MarketGoods.Column[] columns = {MarketGoods.Column.id, MarketGoods.Column.name, MarketGoods.Column.brief, MarketGoods.Column.picUrl, MarketGoods.Column.isNew, MarketGoods.Column.isHot, MarketGoods.Column.counterPrice, MarketGoods.Column.retailPrice};
        // newGoodsList[]:market_goods表中is_new
        List<NewHotGoodsListVo> newGoodsList = new ArrayList<>();
        MarketGoodsExample newGoodsExample = new MarketGoodsExample();
        newGoodsExample.or().andDeletedEqualTo(false).andIsNewEqualTo(true);

        List<MarketGoods> newGoods = goodsMapper.selectByExampleSelective(newGoodsExample, columns);
        // 限制6个
        for (MarketGoods newGood : newGoods) {
            if (newGoodsList.size() == 6){
                break;
            }
            NewHotGoodsListVo goodsListVo = new NewHotGoodsListVo();
            goodsListVo.setId(newGood.getId());
            goodsListVo.setName(newGood.getName());
            goodsListVo.setBrief(newGood.getBrief());
            goodsListVo.setPicUrl(newGood.getPicUrl());
            goodsListVo.setIsNew(newGood.getIsNew());
            goodsListVo.setIsHot(newGood.getIsHot());
            goodsListVo.setCounterPrice(newGood.getCounterPrice());
            goodsListVo.setRetailPrice(newGood.getRetailPrice());
            // 加入
            newGoodsList.add(goodsListVo);
        }


        // 加入map中
        result.put("newGoodsList", newGoodsList);

        // hotGoodsList[]:market_goods表中is_hot
        List<NewHotGoodsListVo> hotGoodsList = new ArrayList<>();
        MarketGoodsExample hotGoodsExample = new MarketGoodsExample();
        newGoodsExample.or().andDeletedEqualTo(false).andIsHotEqualTo(true);
        List<MarketGoods> hotGoods = goodsMapper.selectByExampleSelective(hotGoodsExample, columns);

        for (MarketGoods hotGood : hotGoods) {

            NewHotGoodsListVo goodsListVo = new NewHotGoodsListVo();
            if (hotGoodsList.size() == 6){
                break;
            }
            goodsListVo.setId(hotGood.getId());
            goodsListVo.setName(hotGood.getName());
            goodsListVo.setBrief(hotGood.getBrief());
            goodsListVo.setPicUrl(hotGood.getPicUrl());
            goodsListVo.setIsNew(hotGood.getIsNew());
            goodsListVo.setIsHot(hotGood.getIsHot());
            goodsListVo.setCounterPrice(hotGood.getCounterPrice());
            goodsListVo.setRetailPrice(hotGood.getRetailPrice());
            hotGoodsList.add(goodsListVo);

        }

        result.put("hotGoodsList", hotGoodsList);

        // couponList[]:coupon表
        List<CouponListVo> couponList = new ArrayList<>();
        MarketCouponExample couponExample = new MarketCouponExample();
        couponExample.or().andDeletedEqualTo(false);
        List<MarketCoupon> coupons = couponMapper.selectByExampleSelective(couponExample, MarketCoupon.Column.id, MarketCoupon.Column.name, MarketCoupon.Column.desc, MarketCoupon.Column.tag, MarketCoupon.Column.discount, MarketCoupon.Column.min, MarketCoupon.Column.days);

        for (MarketCoupon coupon : coupons) {
            if (couponList.size() == 3){
                break;
            }
            CouponListVo couponListVo = new CouponListVo();
            couponListVo.setId(coupon.getId());
            couponListVo.setName(coupon.getName());
            couponListVo.setDesc(coupon.getDesc());
            couponListVo.setTag(coupon.getTag());
            couponListVo.setDiscount(coupon.getDiscount());
            couponListVo.setMin(coupon.getMin());
            couponListVo.setDays(coupon.getDays());

            couponList.add(couponListVo);
        }


        result.put("couponList", couponList);

        // channel[]：category
        List<CategoryListVo> channelList = new ArrayList<>();
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        categoryExample.or().andDeletedEqualTo(false);
        List<MarketCategory> categories = categoryMapper.selectByExampleSelective(categoryExample, MarketCategory.Column.id, MarketCategory.Column.name, MarketCategory.Column.iconUrl);

        for (MarketCategory category : categories) {
            if (channelList.size() == 10){
                break;
            }
            CategoryListVo categoryListVo = new CategoryListVo();
            categoryListVo.setId(category.getId());
            categoryListVo.setName(category.getName());
            categoryListVo.setIconUrl(category.getIconUrl());

            channelList.add(categoryListVo);
        }

        result.put("channel", channelList);

        // adList[]：ad表
        MarketAdExample adExample = new MarketAdExample();
        adExample.or().andDeletedEqualTo(false);
        List<MarketAd> banner = adMapper.selectByExample(adExample);

        result.put("banner", banner);

        // brandList[]：brand表
        List<BrandListVo> brandList = new ArrayList<>();
        MarketBrandExample brandExample = new MarketBrandExample();
        brandExample.or().andDeletedEqualTo(false);
        List<MarketBrand> brands = brandMapper.selectByExampleSelective(brandExample, MarketBrand.Column.id, MarketBrand.Column.name, MarketBrand.Column.desc, MarketBrand.Column.picUrl, MarketBrand.Column.floorPrice);

        for (MarketBrand brand : brands) {
            if (brandList.size() == 6){
                break;
            }
            BrandListVo brandListVo = new BrandListVo();
            brandListVo.setId(brand.getId());
            brandListVo.setName(brand.getName());
            brandListVo.setDesc(brand.getDesc());
            brandListVo.setPicUrl(brand.getPicUrl());
            brandListVo.setFloorPrice(brand.getFloorPrice());

            brandList.add(brandListVo);
        }


        result.put("brandList", brandList);

        // topicList[]：topic表
        List<TopicListVo> topicList = new ArrayList<>();
        MarketTopicExample topicExample = new MarketTopicExample();
        topicExample.or().andDeletedEqualTo(false);
        List<MarketTopic> topics = topicMapper.selectByExampleSelective(topicExample, MarketTopic.Column.id, MarketTopic.Column.title, MarketTopic.Column.subtitle, MarketTopic.Column.price, MarketTopic.Column.readCount, MarketTopic.Column.picUrl);

        for (MarketTopic topic : topics) {

            TopicListVo topicListVo = new TopicListVo();

            topicListVo.setId(topic.getId());
            topicListVo.setTitle(topic.getTitle());
            topicListVo.setSubtitle(topic.getSubtitle());
            topicListVo.setPrice(topic.getPrice());
            topicListVo.setReadCount(topic.getReadCount());
            topicListVo.setPicUrl(topic.getPicUrl());

            topicList.add(topicListVo);
        }

        result.put("topicList", topicList);

        // 新品推荐
        List<FloorGoodsListVo> floorGoodsList = new ArrayList<>();

        MarketCategoryExample floorCategoryExample = new MarketCategoryExample();
        // 如果指定类别在此处添加条件
        List<String> floors = new ArrayList<>();
        floors.add("锅具");
        floors.add("魔兽世界");
        floorCategoryExample.or().andDeletedEqualTo(false).andNameIn(floors);
        List<MarketCategory> floorCategories = categoryMapper.selectByExampleSelective(floorCategoryExample, MarketCategory.Column.id, MarketCategory.Column.name);
        for (MarketCategory floorCategory : floorCategories) {
            FloorGoodsListVo floorGoodsListVo = new FloorGoodsListVo();
            Integer categoryId = floorCategory.getId();
            floorGoodsListVo.setId(categoryId);
            floorGoodsListVo.setName(floorCategory.getName());

            // 再到goods表找对应的category_id
            List<NewHotGoodsListVo> goodsList = new ArrayList<>();
            MarketGoodsExample floorGoodsExample = new MarketGoodsExample();
            floorGoodsExample.or().andDeletedEqualTo(false).andCategoryIdEqualTo(categoryId);
            List<MarketGoods> floorMarketGoods = goodsMapper.selectByExampleSelective(floorGoodsExample, columns);
            for (MarketGoods floorMarketGood : floorMarketGoods) {
                NewHotGoodsListVo goodsListVo = new NewHotGoodsListVo();
                goodsListVo.setId(floorMarketGood.getId());
                goodsListVo.setName(floorMarketGood.getName());
                goodsListVo.setBrief(floorMarketGood.getBrief());
                goodsListVo.setPicUrl(floorMarketGood.getPicUrl());
                goodsListVo.setIsNew(floorMarketGood.getIsNew());
                goodsListVo.setIsHot(floorMarketGood.getIsHot());
                goodsListVo.setCounterPrice(floorMarketGood.getCounterPrice());
                goodsListVo.setRetailPrice(floorMarketGood.getRetailPrice());

                goodsList.add(goodsListVo);
            }
            floorGoodsListVo.setGoodsList(goodsList);
            floorGoodsList.add(floorGoodsListVo);
        }
        result.put("floorGoodsList", floorGoodsList);

        return result;
    }
}
